package seGobangClient.uiSystem3D;
import com.sun.j3d.utils.geometry.Text2D;
import javax.media.j3d.*;
import javax.vecmath.*;

import com.sun.j3d.utils.universe.*;
import seGobangClient.gaming.*;
import seGobangResources.PathRes;
/**
 *
 * @author plus
 */
public class GamingScene3D implements IScene3D, seGobangClient.gaming.IGamingScene{
    private static BranchGroup GroupRoot,DBRoot;
    private static RobotGroup GroupRobot;
    private static BehaviorFactory Behaviors=new BehaviorFactory();
       
    private void initDashBoard() {
        DBRoot = new BranchGroup();
        DBRoot.setCapability(BranchGroup.ALLOW_DETACH);

        TransformGroup TGMain = getTG((Node)null,.6f,-0.1f,-2f);                         //整体信息面板位置
        
        TransformGroup TGInfoBlack=null;
        try{    
            TGInfoBlack=getTG(ObjectFactory.createTexturedSurface(
                    0.5f, 0.35f, new Color3f(1f,1f,1f), 0.2f, PathRes.getPath("Texture/Gaming/backBlack3D")),
                    0f,.5f,0f);
            TGInfoBlack.addChild(getTG(
                    new Text2D("玩家1",new Color3f(1f,1f,1f),"华文行楷",56,java.awt.Font.PLAIN),
                    -.28f,-0.01f,0.4f));
            TGInfoBlack.addChild(getTG(
                    new Text2D("第00手",new Color3f(1f,1f,1f),"微软雅黑",40,java.awt.Font.PLAIN),
                    -0.02f,.04f,0.13f));
            TGInfoBlack.addChild(getTG(
                    new Text2D("悔棋机会10次",new Color3f(1f,1f,1f),"微软雅黑",52,java.awt.Font.PLAIN),
                    -.25f,-0.02f,0.13f));
            TGInfoBlack.addChild(getTG(
                    new Text2D("剩余时间5分5秒",new Color3f(1f,1f,1f),"微软雅黑",52,java.awt.Font.PLAIN),
                    -.25f,-0.08f,0.13f));
            
            TransformGroup TGBlackButts = getTG((Node)null,-.27f,-0.15f,0.2f);
            TGBlackButts.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
            TGInfoBlack.addChild(TGBlackButts);
            TGBlackButts.addChild(getTG(ObjectFactory.createTexturedSurface(
                    0.2f, 0.08f, new Color3f(1f,1f,1f), 0f, PathRes.getPath("Texture/Gaming/RegretB"),"1PREG"),
                    ObjectFactory.createTexturedSurface(
                    0.2f, 0.08f, new Color3f(1f,1f,1f), 0f, PathRes.getPath("Texture/Gaming/RegretA"),"1PREG"),
                    0.1f,0f,0f));
            TGBlackButts.addChild(getTG(ObjectFactory.createTexturedSurface(
                    0.2f, 0.08f, new Color3f(1f,1f,1f), 0f, PathRes.getPath("Texture/Gaming/SurrenderB"),"1PSUR"),
                    ObjectFactory.createTexturedSurface(
                    0.2f, 0.08f, new Color3f(1f,1f,1f), 0f, PathRes.getPath("Texture/Gaming/SurrenderA"),"1PSUR"),
                    0.31f,0f,0f));
            
        }catch(Exception e){
            e.printStackTrace();
        }
        
        TransformGroup TGInfoWhite = null;
        try{    
            TGInfoWhite=getTG(ObjectFactory.createTexturedSurface(
                    0.62f, 0.45f, new Color3f(1f,1f,1f), 0.67f, PathRes.getPath("Texture/Gaming/backWhite3D")),
                    0.14f,0.05f,-.5f);
            TGInfoWhite.addChild(getTG(
                    new Text2D("玩家2",new Color3f(1f,1f,1f),"华文行楷",56,java.awt.Font.PLAIN),
                    -.42f,.09f,0.9f));
            TGInfoWhite.addChild(getTG(
                    new Text2D("第00手",new Color3f(1f,1f,1f),"微软雅黑",40,java.awt.Font.PLAIN),
                    -0.16f,.08f,0.63f));
            TGInfoWhite.addChild(getTG(
                    new Text2D("悔棋机会10次",new Color3f(1f,1f,1f),"微软雅黑",52,java.awt.Font.PLAIN),
                    -.39f,0.02f,0.63f));
            TGInfoWhite.addChild(getTG(
                    new Text2D("剩余时间5分5秒",new Color3f(1f,1f,1f),"微软雅黑",52,java.awt.Font.PLAIN),
                    -.39f,-0.04f,0.63f));
            
            TransformGroup TGWhiteButts = getTG((Node)null,-.41f,-0.1f,0.2f);
            TGWhiteButts.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
            TGInfoWhite.addChild(TGWhiteButts);
            TGWhiteButts.addChild(getTG(ObjectFactory.createTexturedSurface(
                    0.2f, 0.08f, new Color3f(1f,1f,1f), 0f, PathRes.getPath("Texture/Gaming/RegretB"),"2PREG"),
                    ObjectFactory.createTexturedSurface(
                    0.2f, 0.08f, new Color3f(1f,1f,1f), 0f, PathRes.getPath("Texture/Gaming/RegretA"),"2PREG"),
                    0.1f,0f,0.5f));
            TGWhiteButts.addChild(getTG(ObjectFactory.createTexturedSurface(
                    0.2f, 0.08f, new Color3f(1f,1f,1f), 0f, PathRes.getPath("Texture/Gaming/SurrenderB"),"2PSUR"),
                    ObjectFactory.createTexturedSurface(
                    0.2f, 0.08f, new Color3f(1f,1f,1f), 0f, PathRes.getPath("Texture/Gaming/SurrenderA"),"2PSUR"),
                    0.31f,0f,0.5f));
        }catch(Exception e){
            e.printStackTrace();
        }
        
        TransformGroup TGButtAbandon = null;
        try{    
            TGButtAbandon = getTG((Node)null,0f,-0.4f,0f);
            TGButtAbandon.addChild(getTG((Node)ObjectFactory.createTexturedSurface(
                    0.2f, 0.08f, new Color3f(1f,1f,1f), 0f, PathRes.getPath("Texture/Gaming/AbandonB"),"ABA"),
                    ObjectFactory.createTexturedSurface(
                    0.2f, 0.08f, new Color3f(1f,1f,1f), 0f, PathRes.getPath("Texture/Gaming/AbandonA"),"ABA"),
                    0.1f,0f,0.0f));
        }catch(Exception e){
            e.printStackTrace();
        }
        
        TGMain.addChild(TGInfoBlack);
        TGMain.addChild(TGInfoWhite);
        TGMain.addChild(TGButtAbandon);
        
        DBRoot.addChild(TGMain);
        
        Behavior PickGamingMenu = Behaviors.createPickGamingMenuBehavior(DBRoot, TGMain);
            PickGamingMenu.setSchedulingBounds(new BoundingSphere(new Point3d(0d,0d,0d),100));
            
        DBRoot.addChild(PickGamingMenu);
        
        DBRoot.compile();
        System.out.println("DashBoard Inited");
    }
    
    private TransformGroup getTG(Node child,float x,float y,float z){
        Transform3D transRes = new Transform3D();                                
        transRes.set(new Vector3f(x, y, z));
        TransformGroup TGRes = new TransformGroup(transRes);
        if (child!=null) TGRes.addChild(child);
        return TGRes;
    }
    private TransformGroup getTG(Node child1,Node child2,float x,float y,float z){
        Transform3D transRes = new Transform3D();                                
        transRes.set(new Vector3f(x, y, z));
        TransformGroup TGRes = new TransformGroup(transRes);
        TGRes.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        if (child1!=null) TGRes.addChild(child1);
        if (child2!=null) TGRes.addChild(child2);
        return TGRes;
    }
    private TransformGroup getTG(Text2D child,float x,float y,float z){
        Transform3D transRes = new Transform3D();                                
        transRes.set(new Vector3f(x, y, z));
        Transform3D transText=new Transform3D();
        transText.setScale(0.25);
        transRes.mul(transText);
        TransformGroup TGRes = new TransformGroup(transRes);
        if (child!=null) {
            ((Appearance)child.getAppearance()).setCapability(Appearance.ALLOW_TEXTURE_WRITE);
            TGRes.addChild(child);
        }
        return TGRes;
    }
    
    private void initChessBoard(){
        GroupRoot=new BranchGroup();
        GroupRoot.setCapability(BranchGroup.ALLOW_DETACH);
        GroupRoot.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
        GroupRoot.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
        
        GroupRoot.addChild(ObjectFactory.createChessboard());
        
        TransformGroup TGChesses=getTG((Node)null,0f,0f,0f);
        for (int i=0; i<=14; i++)
            for (int j=0; j<=14; j++)
                TGChesses.addChild(ObjectFactory.createChessTG(new Color3f(1f,1f,1f), (float)i-7, 0f, (float)j-7));
        GroupRoot.addChild(TGChesses);
        
        TransformGroup TGIndict=new TransformGroup();
            TGIndict.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
            TGIndict.addChild(ObjectFactory.createChessIndicator());
        GroupRoot.addChild(TGIndict);
        
        Behavior PickChessBoard = Behaviors.createPickChessBoardBehavior(GroupRoot, TGIndict);
            PickChessBoard.setSchedulingBounds(new BoundingSphere(new Point3d(0d,0d,0d),100));
            
        GroupRoot.addChild(PickChessBoard);
        
        GroupRoot.compile();
        
    }

    public void setChess(Point2i pos, byte ChessFlag) {
        TransformGroup TGChess=(TransformGroup) ((Group)GroupRoot.getChild(1)).getChild(
                (pos.x+7)*15+(pos.y+7));
        Transform3D t3d=new Transform3D();
        switch (ChessFlag){
            case 0:
                t3d.set(new Vector3f((float)pos.x, -1f, (float)pos.y));
                break;
            case -1:
                t3d.set(new Vector3f((float)pos.x, 0f, (float)pos.y));
                ((Shape3D) (TGChess.getChild(0))).getAppearance().getMaterial().setAmbientColor(new Color3f(0f, 0f, 0f));
                ((Shape3D) (TGChess.getChild(0))).getAppearance().getMaterial().setDiffuseColor(new Color3f(0f, 0f, 0f));
                break;
            case 1:
                ((Shape3D) (TGChess.getChild(0))).getAppearance().getMaterial().setAmbientColor(new Color3f(1f, 1f, 1f));
                ((Shape3D) (TGChess.getChild(0))).getAppearance().getMaterial().setDiffuseColor(new Color3f(1f, 1f, 1f));
                t3d.set(new Vector3f((float)pos.x, 0f, (float)pos.y));
                break;
        }
        TGChess.setTransform(t3d);
    }

    public void setChessBoardPickable(boolean isPickable) {
        ((Node)(((Group)GroupRoot.getChild(0)).getChild(0))).setPickable(isPickable);
    }

    public void setCurrentPlayer(byte ChessFlag) {
        Plane2D plane=null;
        Group g1,g2;
        if (ChessFlag==-1){
            g1=(Group)DBRoot.getChild(0);
            g2=(Group)g1.getChild(0);
            plane=(Plane2D)g2.getChild(0);
            plane.setAlpha(0.2f);
            
            g1=(Group)DBRoot.getChild(0);
            g2=(Group)g1.getChild(1);
            plane=(Plane2D)g2.getChild(0);
            plane.setAlpha(0.67f);
        }else{
            g1=(Group)DBRoot.getChild(0);
            g2=(Group)g1.getChild(0);
            plane=(Plane2D)g2.getChild(0);
            plane.setAlpha(0.67f);
            
            g1=(Group)DBRoot.getChild(0);
            g2=(Group)g1.getChild(1);
            plane=(Plane2D)g2.getChild(0);
            plane.setAlpha(0.2f);
        }
    }

    public void updatePlayerInfo(IPlayer blackPlayer, IPlayer whitePlayer) {
        Group gBlack,gWhite;
        gBlack=gWhite=(Group)DBRoot.getChild(0);
        gBlack=(Group)gBlack.getChild(0);
        gWhite=(Group)gWhite.getChild(1);
        
        getText2D((TransformGroup)gBlack.getChild(1)).setString(blackPlayer.getPlayerName());
        getText2D((TransformGroup)gBlack.getChild(2)).setString("第"+blackPlayer.getTurns()+"手");
        if(blackPlayer.getLastRegrets()>=0)
            getText2D((TransformGroup)gBlack.getChild(3)).setString("悔棋机会"+blackPlayer.getLastRegrets()+"次");
        else
            getText2D((TransformGroup)gBlack.getChild(3)).setString("");
        if(blackPlayer.getLastTime()>=0)
            getText2D((TransformGroup)gBlack.getChild(4)).setString("剩余时间"+blackPlayer.getLastTime()/60+"分"+
                blackPlayer.getLastTime()%60+"秒");
        else
            getText2D((TransformGroup)gBlack.getChild(4)).setString("");
        
        getText2D((TransformGroup)gWhite.getChild(1)).setString(whitePlayer.getPlayerName());
        getText2D((TransformGroup)gWhite.getChild(2)).setString("第"+whitePlayer.getTurns()+"手");
        if(blackPlayer.getLastRegrets()>=0)
            getText2D((TransformGroup)gWhite.getChild(3)).setString("悔棋机会"+whitePlayer.getLastRegrets()+"次");
        else
            getText2D((TransformGroup)gWhite.getChild(3)).setString("");
        if(whitePlayer.getLastTime()>=0)
            getText2D((TransformGroup)gWhite.getChild(4)).setString("剩余时间"+whitePlayer.getLastTime()/60+"分"+
                whitePlayer.getLastTime()%60+"秒");
        else
            getText2D((TransformGroup)gWhite.getChild(4)).setString("");
        
        if (blackPlayer instanceof LocalPlayer){
            Transform3D trans=new Transform3D();
            trans.set(new Vector3f(-.27f,-0.15f,0.2f));
            ((TransformGroup)((TransformGroup)gBlack.getChild(5))).setTransform(trans);
            if (blackPlayer.getLastRegrets()==0){
                trans=new Transform3D();
                trans.set(new Vector3f(5f,5f,0f));
                ((TransformGroup)((TransformGroup)((TransformGroup)gBlack.getChild(5))).getChild(0)).
                        setTransform(trans);
            }else{
                trans=new Transform3D();
                trans.set(new Vector3f(0.1f,0f,0f));
                ((TransformGroup)((TransformGroup)((TransformGroup)gBlack.getChild(5))).getChild(0)).
                        setTransform(trans);
            }
        }else{
            Transform3D trans=new Transform3D();
            trans.set(new Vector3f(5f,5f,0f));
            ((TransformGroup)((TransformGroup)gBlack.getChild(5))).setTransform(trans);
        }
        
        if (whitePlayer instanceof LocalPlayer){
            Transform3D trans=new Transform3D();
            trans.set(new Vector3f(-.41f,-0.1f,0.2f));
            ((TransformGroup)((TransformGroup)gWhite.getChild(5))).setTransform(trans);
            if (whitePlayer.getLastRegrets()==0){
                trans=new Transform3D();
                trans.set(new Vector3f(5f,5f,0f));
                ((TransformGroup)((TransformGroup)((TransformGroup)gWhite.getChild(5))).getChild(0)).
                        setTransform(trans);
            }else{
                trans=new Transform3D();
                trans.set(new Vector3f(0.1f,0f,0.5f));
                ((TransformGroup)((TransformGroup)((TransformGroup)gWhite.getChild(5))).getChild(0)).
                        setTransform(trans);
            }
        }else{
            Transform3D trans=new Transform3D();
            trans.set(new Vector3f(5f,5f,0f));
            ((TransformGroup)((TransformGroup)gWhite.getChild(5))).setTransform(trans);
        }
    }

    public void initPlayerInfo(IPlayer blackPlayer, IPlayer whitePlayer) {
        updatePlayerInfo(blackPlayer,whitePlayer);
        if (whitePlayer instanceof AIPlayer){
            if(GroupRobot==null) {
                GroupRobot=new RobotGroup();
            }
            GroupRoot.addChild(GroupRobot);
        }else{
            if(GroupRobot!=null) GroupRobot.detach();
        }
            
    }

    public void AIFx(Point2i pos, int FxID) {
        //SceneManager.MyCurrentController.doEvent("ChooseChessBoard", pos);
        if (FxID==this.AIFx_PUT)
            this.GroupRobot.AIPutChess(pos);
        if (FxID==this.AIFx_WIN)
            this.GroupRobot.AIWin();
    }
    
    private Text2D getText2D(TransformGroup TGText){
        return (Text2D) TGText.getChild(0);
    }
    
    public void setUpScene() {
        if (GroupRoot==null) initChessBoard();
        if (DBRoot==null) initDashBoard();
        SceneManager.objRoot.addChild(GroupRoot);
        SceneManager.dashBoardRoot.addChild(DBRoot);
        
        SceneManager.setView(new Vector3f(-10f,28f,3f),
                new Vector3f((float)(-Math.PI*.5),(float)(-Math.PI*.1),(float)-Math.PI/2));
    }

    public void shutDown() {
        if(GroupRobot!=null) GroupRobot.detach();
        GroupRoot.detach(); //与主场景脱离
        DBRoot.detach();
        
    }
    
}